package com.example.douyin.register;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.text.method.ReplacementTransformationMethod;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.douyin.R;
import com.example.douyin.databinding.ActivityRegisterBinding;
import com.example.lib_base_mvvm.baseMVVM.BaseView.BaseActivity;
import com.example.lib_base_mvvm.entity.UserEntity;

import java.util.Random;

@Route(path = "/app/RegisterActivity")
public class RegisterActivity extends BaseActivity<ActivityRegisterBinding,RegisterViewModel> {
    @Autowired
    String name;

    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        dataBinding.etUsername.setText(name);//登录失败后，将用户名传给注册页面
        dataBinding.getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = new Random().nextInt(9999);
                ToastUtils.showLong(i);
            }
        });

        dataBinding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //3个 EditText必填，并且 两次密码需要一致    用户名需要时手机号
                String username = dataBinding.etUsername.getText().toString();
                String password = dataBinding.etPassword.getText().toString();
                String repassword = dataBinding.etRepassword.getText().toString();
                if(username.isEmpty()||password.isEmpty()||repassword.isEmpty()){
                    ToastUtils.showLong("用户名和密码不能为空");

                }else if(!password.equals(repassword)){
                    ToastUtils.showLong("两次密码不一致");
                }else if(!RegexUtils.isMobileSimple(username)){
                    ToastUtils.showLong("用户名必须是手机号");
                }else{
                    viewModel.register(username,password,repassword);
                }
            }
        });
    }

    @Override
    public void initData() {

        viewModel.success.observe(this, new Observer<UserEntity>() {
            @Override
            public void onChanged(UserEntity entity) {
                if(entity.getCode()==200){
                    //跳转到登录界面
                    ARouter.getInstance().build("/app/MainActivity").withSerializable("user",entity.getData()).navigation();
                }else{
                    ToastUtils.showLong(entity.getMessage());
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

    }

    @Override
    public Class<RegisterViewModel> getViewModel() {
        return RegisterViewModel.class;
    }

}